$(document).ready(function () {
    //预读数据库的部门信息，添加下拉框内容
    $.ajax({
        type: "get",
        url: "/ClassServlet?action=getAllDepartmentInfo",
        dataType: "json",
        success: function (data) {
            $("#class_department_id").html("");
            for (var i in data) {
                $("#class_department_id").append("<option value='" + data[i].department_id + "'>" + data[i].department_name + "</option>");
            }
        },
    });
    //预读数据库的辅导员信息，添加下拉框内容
    $.ajax({
        type: "get",
        url: "/ClassServlet?action=getAllTeacherInfo",
        dataType: "json",
        success: function (data) {
            $("#class_teacher_id").html("");
            for (var i in data) {
                $("#class_teacher_id").append("<option value='" + data[i].teacher_id + "'>" + data[i].teacher_name + "</option>");
            }
        },
    });
    //预设入学年份的值
    $("#class_enter_year").attr("value", new Date().getFullYear());
});

//验证班级id是否已被注册
function varityClass_id() {
    var class_id = $("#class_id").val();
    $.ajax({
        type: "post",
        url: "/ClassServlet",
        data: "action=varityClass_id&class_id=" + class_id,
        dataType: "text",
        success: function (data) {
            $("#notice_class_id").html("");
            if (data == "false") {
                $("#notice_class_id").append("班级ID已被注册");
                $("#notice_class_id").css("color", "red");
            }
            else {
                $("#notice_class_id").append("班级ID可用");
                $("#notice_class_id").css("color", "green");
            }

        }
    })
}

//添加班级
function addClass() {
    var class_id = $("#class_id").val();
    var class_name = $("#class_name").val();
    var class_department_id = $("#class_department_id").val();
    var class_teacher_id = $("#class_teacher_id").val();
    var class_enter_year = $("#class_enter_year").val();
    $.ajax({
        type: "post",
        url: "/ClassServlet",
        data: "action=addClass&class_id=" + class_id + "&class_name=" + class_name + "&class_department_id=" + class_department_id + "&class_teacher_id=" + class_teacher_id + "&class_enter_year=" + class_enter_year,
        dataType: "text",
        success: function (data) {
            alert(data);
        }
    })
}